
package com.rt.appauthorize;
import java.util.Iterator;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
public class AppAuthPostRedirectUtil
{
    Map<String, String> parameter;
    HttpServletResponse response;
    
    public AppAuthPostRedirectUtil(final HttpServletResponse response) {
        this.parameter = new HashMap<String, String>();
        this.response = response;
    }
    
    public static AppAuthPostRedirectUtil builderPostRedirect(final HttpServletResponse response) {
        return new AppAuthPostRedirectUtil(response);
    }
    
    public void setParameter(final String key, final String value) {
        this.parameter.put(key, value);
    }
    
    public void sendByPost(final String url) {
        this.response.setContentType("text/html");
        PrintWriter out = null;
        try {
            out = this.response.getWriter();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(" <HEAD>");
        out.println(" <TITLE>页面加载中，请稍后</TITLE>");
        out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html charset=UTF-8\">\n");
        out.println(" </HEAD>");
        out.println(" <BODY>");
        out.println("<form id=\"submitForm\" name=\"submitForm\" action=\"\" method=\"post\">");
        for (final String key : this.parameter.keySet()) {
            out.println("<input type=\"hidden\" name=\"" + key + "\" value=\"" + this.parameter.get(key) + "\"/>");
        }
        out.println("</from>");
        out.println("<script type=\"application/javascript\">document.getElementById('submitForm').action=getContextPath()+'" + url + "';window.document.submitForm.submit();function getContextPath(){var pathName=document.location.pathname;var index=pathName.substr(1).indexOf(\"/\");var result=pathName.substr(0,index+1);return result};</script> ");
        out.println(" </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}
